<?php
/**
 * User:    Yejia
 * Email:   ye91@foxmail.com
 */


require_once __DIR__ . DIRECTORY_SEPARATOR;

// 设置起始爬取地址
$requestUrl = json_encode(['url' => 'huaban.com', 'path' => '/partner/uc/aimeinv/pins/']);

$imageUrl = 'hbimg.b0.upaiyun.com';
$dir = __DIR__ . DIRECTORY_SEPARATOR . 'images/';
// 请求
$requestChan = new Chan(100);
// 请求结果解析
$parserChan = new Chan(100);
// 解析结果下载
$downloadChan = new Chan(100);

// 初始化任务投递
go(function () use ($requestChan, $requestUrl) {
    $requestChan->push($requestUrl);
});
//启动100 个协程工作
for ($i = 1; $i <= 100; $i++) {
   // 启动携程获取投递任何的html标签 并进行投递
    go(function () use ($requestChan, $parserChan) {
        while (true) {
                $url = json_decode($requestChan->pop(), true);
                get_html($url, $parserChan);
        }
    });
   // 解析html标签中的需要的连接地址 并进行投递
    go(function () use ($parserChan, $requestChan, $downloadChan, $requestUrl) {
        while (true) {
            $html = $parserChan->pop();
            parse($html, $downloadChan, $requestChan, $requestUrl);
        }
    });
   // 下载img 地址到本地
    go(function () use ($downloadChan, $dir, $imageUrl) {
        while (true) {
            download($downloadChan->pop(), $dir, $imageUrl);
        }
    });

}

swoole_event_wait();

// 获取html代码
function get_html(array $url, Chan $chan)
{
    $chan->push(asynsHttp::get($url));
}

// 执行下载
function download(string $url, string $dir, string $imageUrl)
{
    if (!is_dir($dir)) {
        mkdir($dir);
    }
    if (!strpos($url, '-')) {
        return;
    }
    if (substr($url, 0, 2) == '//') {
        $url = 'http:' . $url;
    }
    asynsHttp::download($imageUrl ,$url, $dir .$url . '.png');
    echo $dir, $url, '.png', PHP_EOL;
}

//正则解析html标签
function parse(string $html, Chan $downloadChan, Chan $requestChan, string $requestUrl)
{
    $requestUrl = json_decode($requestUrl,true);
    preg_match_all('/"key":"(.*?)"/', $html, $images);
    if (!empty($images) && !empty($images[1])) {
        foreach ($images[1] as $image) {
            $downloadChan->push($image);
        }
    }

    preg_match_all('/"pin_id":(\d+),/', $html, $next);
    if (!empty($next) && !empty($next[1])) {
        $requestUrl['path']=$requestUrl['path']."?max=" . end($next[1]) . "&limit=8&wfl=1";
        $requestChan->push(json_encode($requestUrl));
    }
}


